package br.iars.manager.util;

import br.iars.model.entity.Context;
import br.iars.model.entity.RateType;

public class RateConverter {

	public static final Float MIN_UNCONTEXTUALIZED_RATE = 1F;
	public static final Float MAX_UNCONTEXTUALIZED_RATE = 10F;

	public static Float getContextualizedValue(Context context, Float rate) {
		Float convertedRate = convert(MIN_UNCONTEXTUALIZED_RATE, MAX_UNCONTEXTUALIZED_RATE, context.getMinRate(), context.getMaxRate(), rate);
		if (context.getRateType().equals(RateType.INTEGER)) {
			convertedRate = (float) Math.round(convertedRate);
		}
		return convertedRate;
	}

	public static Float getUncontextualizedValue(Context context, Float rate) {
		return convert(context.getMinRate(), context.getMaxRate(), MIN_UNCONTEXTUALIZED_RATE, MAX_UNCONTEXTUALIZED_RATE, rate);
	}

	private static Float convert(float fromMinRate, float fromMaxRate, float toMinRate, float toMaxRate, float rate) {
		return ((rate - fromMinRate) / (fromMaxRate - fromMinRate) * (toMaxRate - toMinRate)) + toMinRate;
	}

	public static void main(String[] args) {
		float minRate = 1f;
		float maxRate = 5f;
		float convertedRate = 0;

		float rate = minRate;
		System.out.println();
		while (rate <= maxRate) {
			convertedRate = convert(minRate, maxRate, MIN_UNCONTEXTUALIZED_RATE, MAX_UNCONTEXTUALIZED_RATE, rate);

			System.out.print(rate + "<->" + convertedRate + "   ");
			rate += 0.5f;
		}
		System.out.println();

		rate = MIN_UNCONTEXTUALIZED_RATE;
		System.out.println();
		while (rate <= MAX_UNCONTEXTUALIZED_RATE) {
			convertedRate = convert(MIN_UNCONTEXTUALIZED_RATE, MAX_UNCONTEXTUALIZED_RATE, minRate, maxRate, rate);

			System.out.print(rate + "<->" + convertedRate + "   ");
			rate += 0.5f;
		}
		System.out.println();

		Float teste = 5.0F;
		System.out.println(teste.intValue() != teste);

	}
}
